/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ColorPanel.java
 *
 * Created on Oct 6, 2010, 1:46:19 PM
 */
package project.jpdftools.ui;

import java.awt.Color;
import java.awt.Toolkit;
import java.util.logging.Level;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import project.jpdftools.constants.Const.COLOR_TYPE;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.ui.logic.JPDF;
import project.jpdftools.ui.logic.JPDFTable;
import project.jpdftools.ui.logic.JPDFText;

/**
 *
 * @author Mido
 */
public class ColorPanel extends JDialog {

    private static boolean isComponentVisible = false;
    private JComponent component;
    private JPDF jpdfComponent;
    private int colorType; // determine if the color was a fill color, font color or border color

    /**
     * Creates new form ColorPanel
     */
    public ColorPanel(JComponent component, JPDF jpdf_component, int color_type) {
        initComponents();

        // center the component
        project.jpdftools.toolkit.Toolkit.centerWindow(this, this.getHeight(), this.getWidth());
        this.setAlwaysOnTop(true);

        this.component = component;
        this.jpdfComponent = jpdf_component;
        this.colorType = color_type;

        // set component title
        switch (this.colorType) {
            case (COLOR_TYPE.fill_color):
                this.setTitle(LocalizationManager.getCurrent_resource().getString("COLOR_PANEL_FILL_COLOR_TITLE"));
                break;
            case (COLOR_TYPE.font_color):
                this.setTitle(LocalizationManager.getCurrent_resource().getString("COLOR_PANEL_FONT_COLOR_TITLE"));
                break;
            case (COLOR_TYPE.border_color):
                this.setTitle(LocalizationManager.getCurrent_resource().getString("COLOR_PANEL_BORDER_COLOR_TITLE"));
                break;
        }

        // indicate that the frame is visible
        isComponentVisible = true;
        LoggerManager.getLogger().log(Level.INFO, "Frame shown \"{0}\".", "ColorPanel");
    }

    public static boolean IsComponentVisible() {
        return isComponentVisible;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ch_color_chooser = new javax.swing.JColorChooser();
        bt_ok = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(Toolkit.getDefaultToolkit().getImage("Data/Icons/Soft Scraps Icons/10.png"));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                ColorPanel.this.windowClosing(evt);
            }
        });

        bt_ok.setText(LocalizationManager.getCurrent_resource().getString("OK"));
        bt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_okActionPerformed(evt);
            }
        });

        bt_cancel.setText(LocalizationManager.getCurrent_resource().getString("CANCEL"));
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_ok))
                    .addComponent(ch_color_chooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ch_color_chooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_ok)
                    .addComponent(bt_cancel))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        isComponentVisible = false;
        LoggerManager.getLogger().log(Level.INFO, "Frame hidden \"{0}\".", "ColorPanel");
    }//GEN-LAST:event_bt_cancelActionPerformed

    private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
        // get selected color
        Color selected_color = ch_color_chooser.getColor();
        switch (this.colorType) {
            case (COLOR_TYPE.fill_color):
                // apply color on component
                this.component.setBackground(selected_color);
                if (jpdfComponent instanceof JPDFText) {
                    // cast the object to JPDFText object and apply color on object parameters
                    ((JPDFText) jpdfComponent).setFillColor(selected_color);
                }
                LoggerManager.getLogger().log(Level.INFO, "Component Fill Color changed. Component ID = {0}", jpdfComponent.getId());
                break;
            case (COLOR_TYPE.font_color):
                // apply color on component
                component.setForeground(selected_color);
                if (jpdfComponent instanceof JPDFText) {
                    // cast the object to JPDFText object and apply color on object parameters
                    ((JPDFText) jpdfComponent).setFontColor(selected_color);
                } else if (jpdfComponent instanceof JPDFTable) {
                    // cast the object to JPDFText object and apply color on object parameters
                    ((JPDFTable) jpdfComponent).setFontColor(selected_color);
                }
                LoggerManager.getLogger().log(Level.INFO, "Component Font Color changed. Component ID = {0}", jpdfComponent.getId());
                break;
            case (COLOR_TYPE.border_color):
                // apply color on component
                if (((JPDFText) jpdfComponent).isShowBorder() == true) {
                    component.setBorder(BorderFactory.createLineBorder(selected_color));
                }
                if (jpdfComponent instanceof JPDFText) {
                    // cast the object to JPDFText object and apply color on object parameters
                    ((JPDFText) jpdfComponent).setBorderColor(selected_color);
                }
                LoggerManager.getLogger().log(Level.INFO, "Component Border Color changed. Component ID = {0}", jpdfComponent.getId());
                break;
        }
        // close the frame
        this.dispose();
        // indicate that the frame is hidden
        isComponentVisible = false;
    }//GEN-LAST:event_bt_okActionPerformed

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        bt_cancelActionPerformed(null);
    }//GEN-LAST:event_windowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_ok;
    private javax.swing.JColorChooser ch_color_chooser;
    // End of variables declaration//GEN-END:variables
}
